package result

import (
	"github.com/kataras/iris/v12"
)

/**
 * * global web response
 */

func OK(ctx iris.Context) {
	OKData(ctx, nil)
}

func OKData(ctx iris.Context, data any) {
	json(ctx, iris.StatusOK, iris.StatusOK, data, false)
}

func BadRequest(ctx iris.Context, code InnerCode) {
	BadRequestData(ctx, code, nil)
}

func BadRequestData(ctx iris.Context, code InnerCode, data any) {
	json(ctx, iris.StatusBadRequest, code, data, false)
}

func InternalServerError(ctx iris.Context) {
	Other(ctx, iris.StatusInternalServerError, iris.StatusInternalServerError)
}

// 400=请求参数错误
// 401=未认证
// 403=已认证,但未授权限
func Other(ctx iris.Context, status int, code InnerCode) {
	OtherData(ctx, status, code, nil)
}

func OtherData(ctx iris.Context, status int, code InnerCode, data any) {
	json(ctx, status, code, data, true)
}

func VxeTableData(ctx iris.Context, pageSize, currentPage int, total int64, rows, other any) {
	var ok = iris.StatusOK
	ctx.StatusCode(ok)
	json(ctx, ok, InnerCode(ok), iris.Map{
		"page": iris.Map{
			"pageSize":    pageSize,
			"currentPage": currentPage,
			"total":       total,
		},
		"result": rows,
		"other":  other,
	}, false)
}

// send to client of json data
func json(ctx iris.Context, status int, code InnerCode, data any, isTip bool) {
	ctx.StatusCode(status) // RFC
	ctx.JSON(iris.Map{
		"code":   code,
		"msg":    InnerCode(code).message(),
		"result": data,
		"reason": "",    // micro server, transport this field
		"tip":    isTip, // client is tip?
	})
}
